<?php

/**
 * 图片投放管理MODEL类
 * 
 * @author Dean
 *
 */
class picModel extends Model{
	
	//操作的本类表名
	private $tableName = 'launch_picture';
	private $additiontableName = 'launch_picture_addition';
	
	/**
	 * 查询全部投放信息
	 */
	public function findAll($limit=0, $num=20, $params=array()){
		
		$sql = 'select * from '.tblName($this->tableName).'   ORDER BY createTime desc LIMIT '.$limit.' , '.$num;
		
		try {
			$List = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $List;
	}
	
	/**
	 * 统计投放总数量
	 * 
	 * @param unknown_type $type
	 */
	public function countAll($params=array()){
		
		$sql = 'select count(*) as num from '.tblName($this->tableName);
		
		try {
			$number = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $number->num;
	}
	
	/**
	 * 查询单个投放信息，通过投放编号进行查询
	 * 
	 * @param int $id 投放编号
	 */
	public function findOne($id){
		
		$sql = 'select * from '.tblName($this->tableName).' WHERE ID = \''.$id.'\'';
		
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
	
	
	/**
	 * 进行投放新增
	 * 
	 * @param Array $array
	 * @param int $returnID
	 */
	public function add($array, $returnID = 1){
		
		$sql = insertTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $this->db->lastInsertID;
		}else {
			throw new Exception('投放添加失败。');
			return -1;
		}
	}
	
	/**
	 * 进行修改操作
	 * 
	 * @param Array $array
	 * @param Array $where
	 */
	public function modify($array, $where){
		$sql = updateTableSql($this->tableName, $array, $where);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	/**
	 * 进行删除操作
	 * 
	 * @param Array $array
	 */
	public function delete($array){
		
		$sql = deleteTableSql($this->tableName, $array);
		
		try {
			$numRows = $this->db->execute($sql);
		} catch (DBException $e) {
			echo $e->getMessage();
		}
		
		if ($numRows > 0) {
			return $numRows;
		}else {
			return -1;
		}
	}
	
	public function getMaxID(){
		
		$sql = "select id from ".tblName($this->tableName)." order by id desc limit 1";
		
		try {
			$Info = $this->db->getOne($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $Info;
	}
	
	public function getAddition(){
		
		$sql = "select * from ".tblName($this->additiontableName)." order by type";
		
		try {
			$List = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $List;
	}
	
	public function findAllOrderByWeight(){
		$now = date('Y-m-d H:i:s');
		$sql = "select * from ".tblName($this->tableName)." WHERE  (start <= '{$now}' AND end >= '{$now}') AND is_effect = '1'  ORDER BY weight DESC";
		
		try {
			$List = $this->db->getAll($sql);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return $List;
		
	}
	
}

?>